package com.seari.AlarmSystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

import com.seari.AlarmSystem.bean.RedisData;
import com.seari.AlarmSystem.dao.DeployDataDao;
import com.seari.AlarmSystem.utils.JdbcUtils;
import com.seari.AlarmSystem.utils.JedisPoolUtils;
import com.seari.AlarmSystem.utils.PropertiesUtil;

public class DeployDataDaoImpl implements DeployDataDao {
		
	private static final Logger logger = LoggerFactory.getLogger(DeployDataDaoImpl.class);
	private static final String username = PropertiesUtil.getProperty("jdbc.properties","jdbc.username");
	private static final String password = PropertiesUtil.getProperty("jdbc.properties","jdbc.password");
	private static final String driver = PropertiesUtil.getProperty("jdbc.properties","jdbc.driver");
	private static final String url = PropertiesUtil.getProperty("jdbc.properties","jdbc.url");
	// 获取jedis连接池
	private static final Jedis jedis = JedisPoolUtils.getJedis();

	@Override
	public void DeployData() {
		
		// 获取redis缓存中所有DI数据
		Map<String, String> map = jedis.hgetAll("DI");
				
		for (String key : map.keySet()) {
					
		// redis中每个key 以及对应的value 
		System.out.println("key"+ key +": value" + map.get(key));
					
					
					
					
				}
		
		List<RedisData> redisDataList = new ArrayList<RedisData>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = JdbcUtils.getConnection();

			String sql = "select * from data_table where is_alarm = ?";
			ps = conn.prepareStatement(sql);
			// 判断是否为报警点
			ps.setString(0, "0");
			conn.setAutoCommit(false);
			rs = ps.executeQuery();
			conn.commit();
			while(rs.next()){
				//数据读取
				RedisData redisData = new RedisData();
				// hset 的 key 名
				redisData.setTable_name(rs.getString("table_name"));
				// filed key
				redisData.setRedis_address(rs.getInt("redis_address"));
				// 添加
				redisDataList.add(redisData);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				JdbcUtils.release(conn, ps, rs);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		for (RedisData redisData : redisDataList) {
			
			int redis_address = redisData.getRedis_address();
			String table_name = redisData.getTable_name();
			
			System.out.println("redis_address"+redis_address + ": table_name" + table_name);
			
		}
		
		
		
		
		
		
		
	}
	
	
	

}
